package com.moinut.studentattendancemanager.model.dao.impl;

import com.moinut.studentattendancemanager.model.dao.ITeacherDAO;
import com.moinut.studentattendancemanager.model.vo.db.Student;
import com.moinut.studentattendancemanager.model.vo.db.Teacher;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MOILING
 */
public class TeacherDAOImpl extends BaseDAOImpl implements ITeacherDAO {

    public TeacherDAOImpl(Connection connection) {
        super(connection);
    }

    @Override
    public Teacher findLogin(Teacher teacher) throws Exception {
        Teacher getTeacher = null;
        String sql = "SELECT * FROM teacherinfo WHERE teacher_id = ? AND teacher_login_pwd = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, teacher.getTeacherId());
        preparedStatement.setString(2, teacher.getTeacherPassword());
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            getTeacher = new Teacher(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3),
                    resultSet.getString(4), resultSet.getString(5), resultSet.getString(6));
        }
        if (this.preparedStatement != null) {
            this.preparedStatement.close();
        }
        return getTeacher;
    }

    @Override
    public Teacher getTeacherById(String teacherId) throws Exception {
        Teacher teacher = null;
        String sql = "SELECT * FROM teacherinfo WHERE teacher_id = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, teacherId);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            teacher = new Teacher(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3),
                    resultSet.getString(4), resultSet.getString(5), resultSet.getString(6));
        }
        if (this.preparedStatement != null) {
            this.preparedStatement.close();
        }
        return teacher;
    }

    @Override
    public List<Teacher> getAllTeachers() throws Exception {
        List<Teacher> teachers = new ArrayList<>();
        String sql = "SELECT * FROM teacherinfo";
        preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Teacher teacher = new Teacher(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3),
                    resultSet.getString(4), resultSet.getString(5), resultSet.getString(6));
            teachers.add(teacher);
        }
        if (this.preparedStatement != null) {
            this.preparedStatement.close();
        }
        return teachers;
    }

    @Override
    public boolean updateTeacherInfo(String oldId, Teacher newTeacher) throws Exception {
        String sql = "UPDATE teacherinfo " +
                     "SET teacher_id = ?, teacher_name = ?, college_name = ?, faculty_name = ?, teacher_email = ?" +
                     "WHERE teacher_id = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, newTeacher.getTeacherId());
        preparedStatement.setString(2, newTeacher.getTeacherName());
        preparedStatement.setString(3, newTeacher.getCollegeName());
        preparedStatement.setString(4, newTeacher.getFacultyName());
        preparedStatement.setString(5, newTeacher.getTeacherEmail());
        preparedStatement.setString(6, oldId);
        int updateNumber = preparedStatement.executeUpdate();
        if (updateNumber > 0) {
            return true;
        }
        if (this.preparedStatement != null) {
            this.preparedStatement.close();
        }
        return false;
    }

    @Override
    public boolean updateTeacherPassword(String oldId, String newPassword) throws Exception {
        String sql = "UPDATE teacherinfo " +
                     "SET teacher_login_pwd = ?" +
                     "WHERE teacher_id = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, newPassword);
        preparedStatement.setString(2, oldId);
        int updateNumber = preparedStatement.executeUpdate();
        if (updateNumber > 0) {
            return true;
        }
        if (this.preparedStatement != null) {
            this.preparedStatement.close();
        }
        return false;
    }

    @Override
    public boolean updateTeacherEmail(String teacherId, String newEmail) throws Exception {
        String sql = "UPDATE teacherinfo " +
                     "SET teacher_email = ?" +
                     "WHERE teacher_id = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, newEmail);
        preparedStatement.setString(2, teacherId);
        int updateNumber = preparedStatement.executeUpdate();
        if (updateNumber > 0) {
            return true;
        }
        if (this.preparedStatement != null) {
            this.preparedStatement.close();
        }
        return false;
    }
}
